#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<wait.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h> 
#include<pthread.h>

int
main(int argc, char* args[])
{
	if(argc != 2){
		perror("arg error");
		exit(1);
	}

	int ret = mkfifo("fifotest", 0644);
	if(ret < 0){
		perror("mkfifo");
		exit(1);
	}
	int n = 10, pid;
	while(n--){
		pid = fork();
		if(pid < 0){
			perror("fork error");
			exit(1);
		}
		else if((!pid) && (n & 1)){
			int fd = open("fifotest", O_WRONLY);
			char buf[1024];
			sprintf(buf,"this is the %d process\n",n);
			write(fd, buf, strlen(buf));
			sleep(1);
			break;
		}

		else if((!pid) && !(n & 1)){
			int fd =open("fifotest", O_RDONLY);
			char buf[1024];
			read(fd, buf, sizeof(buf));
			printf("%s",buf);
			sleep(1);
			break;
		}
		else{
			//wait(0);
		}
		
	}
	return 0;
}
